package com.tzinfo.tzt.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.util.HttpUtils;
import com.tzinfo.tzt.util.ResultMessage_NoData;
import com.tzinfo.tzt.util.ResultMessage_SelectCar;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/15.
 */
public class GroupPurchaseActivity extends Activity{
    private ImageView imageView;
    ImageLoader image_loader;
    private String cid;
    EditText et_name;
    EditText et_mobile;
    TextView et_carNeed;
    TextView tv_button;
    AlertDialog.Builder a;
    private String[] s = null;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_purchase);


        this.progressDialog = new ProgressDialog(this);
        this.progressDialog.setMessage("加载中");
        this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        cid = bundle.getString("Id");
        new GetData().execute(cid);
        et_name = (EditText)findViewById(R.id.group_purchase_name);
        et_mobile = (EditText)findViewById(R.id.group_purchase_mobile);
        et_carNeed = (TextView)findViewById(R.id.group_purchase_carNeed);
        tv_button = (TextView)findViewById(R.id.group_purchase_button);
//        verticalViewPager = (VerticalViewPager)findViewById(R.id.test_verticalViewPager);
//        verticalViewPager.setAdapter(new TestAdapter(GroupPurchaseActivity.this));

        imageView =(ImageView)findViewById(R.id.group_purchase_imageView);
        image_loader = ImageLoader.getInstance();
        image_loader.displayImage(bundle.getString("image_url"),imageView);

        tv_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new UploadData().execute();
            }
        });

        et_carNeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (s==null)
                    Toast.makeText(GroupPurchaseActivity.this,"没有数据",Toast.LENGTH_SHORT).show();
                else
                    a.show();
            }
        });

    }

    class UploadData extends AsyncTask<String,Void,ResultMessage_NoData>{

        @Override
        protected ResultMessage_NoData doInBackground(String... params) {
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            ResultMessage_NoData result=null;
            pairs.add(new BasicNameValuePair("cid", cid));
            pairs.add(new BasicNameValuePair("mobile", et_mobile.getText().toString()));
            pairs.add(new BasicNameValuePair("Name", et_name.getText().toString()));
            pairs.add(new BasicNameValuePair("CarNeed", et_carNeed.getText().toString()));
            String GetDataUrl = "http://auto.tzinfo.net/mob/api/Group_purchase.ashx";
            String car4sDataString = HttpUtils.doPostMethod(GetDataUrl, pairs);
            Gson gson = new Gson();
            result = gson.fromJson(car4sDataString, ResultMessage_NoData.class);
            return result;
        }

        @Override
        protected void onPostExecute(ResultMessage_NoData result) {
            super.onPostExecute(result);
            if (result.getSuccess() == true){
                Toast.makeText(GroupPurchaseActivity.this, result.getErrorMsg(), Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                {
                    Toast.makeText(GroupPurchaseActivity.this, result.getErrorMsg(), Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }
    }

    class GetData extends AsyncTask<String,Void,ResultMessage_SelectCar>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected ResultMessage_SelectCar doInBackground(String... params) {
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            ResultMessage_SelectCar result=null;
            pairs.add(new BasicNameValuePair("Id",params[0]));
            String GetDataUrl = "http://auto.tzinfo.net/mob/api/Car_Price.ashx";
            String car4sDataString = HttpUtils.doPostMethod(GetDataUrl, pairs);
            Gson gson = new Gson();
            result = gson.fromJson(car4sDataString, ResultMessage_SelectCar.class);
            return result;
        }

        @Override
        protected void onPostExecute(final ResultMessage_SelectCar resultMessage_selectCar) {
            super.onPostExecute(resultMessage_selectCar);
            if (resultMessage_selectCar.getData().size()>0){
                s=new String[resultMessage_selectCar.getData().size()];
                for (int i=0; i< resultMessage_selectCar.getData().size();i++){
                    s[i]=resultMessage_selectCar.getData().get(i).getTitle();
                }
                a =  new AlertDialog.Builder(GroupPurchaseActivity.this)
                        .setItems(s,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        et_carNeed.setText(resultMessage_selectCar.getData().get(which).getTitle());
                                        dialog.dismiss();
                                    }
                                }
                        );
            }
            progressDialog.dismiss();
        }
    }

    public void goBack(View v) {
        this.finish(); //关闭当前Activity
    }
}
